﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HealthAssignDetail
 * 类型：       
 * 功能说明：   
 * 作者：       谈
 * 创建日期：   2007/11/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Commons.Utils;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Search.Dao;
using Shpm.App.Search.Bean;
public partial class App_Input_HealthAssignDetail : System.Web.UI.Page
{
    private const string COMMAND_SEARCH = "DoSearch";

    private IList AllList = new ArrayList();

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(CommonAjaxUtils), this.Page);
        if (IsPostBack == false)
        {
            ViewState["EMPLOYEE_ID"] = Convert.ToString(Context.Items["EMPLOYEE_ID"]);
            ViewState["EMPLOYEE_NAME"] = Convert.ToString(Context.Items["EMPLOYEE_NAME"]);
            ViewState["ParamPageIndex"] = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            ViewState["ParamCommand"] = Convert.ToString(Context.Items["ParamCommand"]);
            ViewState["TXT_ID"] = Convert.ToString(Context.Items["TXT_ID"]);
            ViewState["TXT_NAME"] = Convert.ToString(Context.Items["TXT_NAME"]);

            lblDoctorId.Text = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            lblDoctorName.Text = Convert.ToString(ViewState["EMPLOYEE_NAME"]);

            ViewState["MemberNo"] = txtMemberNo.Text.Trim();
            ViewState["Name"] = txtMemberName.Text.Trim();

            SalesInfoDao dao = new SalesInfoDao();
            SalesInfoBean salesInfoBean = new SalesInfoBean();
            //楼号邦定
            IList list = dao.FindBuilds();
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list.Add(salesInfoBean);
            }
            this.dropBuildNo.DataSource = list;
            this.dropBuildNo.DataBind();


            //楼层邦定
            list = new ArrayList();
            list.Add(salesInfoBean);
            this.dropFloor.DataSource = list;
            this.dropFloor.DataBind();

            //房间号邦定
            this.dropRoom.DataSource = list;
            this.dropRoom.DataBind();
        }
    }
    #endregion

    #region 读取数据
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (COMMAND_SEARCH.Equals(ViewState["ParamCommand"]))
        {
            // 构建检索条件对象
            SetLst1();
            if (ViewState["LstLoad"] == null && Convert.ToString(ViewState["LstLoad"]) == string.Empty)
            {
                SetLst2();
            }
        }
        else
        {
        }
    }
    #endregion

    #region 返回按钮
    protected void btnBlack_Click(object sender, EventArgs e)
    {
        Context.Items["TXT_ID"] = Convert.ToString(ViewState["TXT_ID"]);
        Context.Items["TXT_NAME"] = Convert.ToString(ViewState["TXT_NAME"]);
        Context.Items["ParamPageIndex"] = Convert.ToInt32(ViewState["ParamPageIndex"]);
        Context.Items["ParamCommand"] = Convert.ToString(ViewState["ParamCommand"]);
        ControllerUtils.Transfer(this.Context, "~/App/Input/HealthAssignSrh.aspx", true);
    }
    #endregion

    #region 设置Lst1
    /*
     * 函数名：SetLst1
     * 功能：设置Lst1
     * 参数：无
     * 说明：
     */
    private void SetLst1()
    {
        lst1.Items.Clear();
        HealthAssignBean dataBean = new HealthAssignBean();
        HealthAssignDao Dao = new HealthAssignDao();
        dataBean.DoctorId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        dataBean.MemberNo = Convert.ToString(ViewState["MemberNo"]);
        dataBean.Name = Convert.ToString(ViewState["Name"]);
        dataBean.ApartmentNo = Convert.ToString(ViewState["ApartmentNo"]);
        dataBean.BuildNo = Convert.ToString(ViewState["BuildNo"]);
        dataBean.Floor = Convert.ToString(ViewState["Floor"]);
        IList dataList = Dao.FindByMenber(dataBean);
        if (dataList.Count > 0)
        {
            IEnumerator enm = dataList.GetEnumerator();
            while (enm.MoveNext())
            {
                dataBean = enm.Current as HealthAssignBean;
                ListItem itm = null;
                if (dataBean.EmployeeName != null && dataBean.EmployeeName != string.Empty)
                {
                    itm = new ListItem(dataBean.MemberNo + " " + dataBean.Name + " (" + dataBean.EmployeeName + ")", dataBean.MemberNo);
                }
                else
                {
                    itm = new ListItem(dataBean.MemberNo + " " + dataBean.Name, dataBean.MemberNo);
                }
                lst1.Items.Add(itm);
            }
        }

        if (ViewState["LIST"] != null)
        {
            IList Hlist = (ArrayList)ViewState["LIST"];
            if (Hlist.Count > 0)
            {
                for (int i = 0; i < Hlist.Count; i++)
                {
                    ListItem itm1 = null;
                    string[] str = (string[])Hlist[i];
                    if (CheckListItem(str[1]))
                    {
                        itm1 = new ListItem(str[0], str[1]);
                        lst1.Items.Add(itm1);
                    }
                }
            }
        }

        if (ViewState["LstLoad"] != null && Convert.ToString(ViewState["LstLoad"]) != string.Empty)
        {
            CheckLst1();
        }
    }
    #endregion

    #region 在Lst1中检索并删除相同的数据
    /*
     * 函数名：CheckLst1
     * 功能：在Lst1中检索并删除相同的数据
     * 参数：无
     * 说明：
     */
    private void CheckLst1()
    {
        if (lst1.Items.Count > 0)
        {
            IEnumerator enm = lst1.Items.GetEnumerator();
            int i = 0;
            int k = 0;
            int iCnt = lst1.Items.Count;
            string[] str = new string[iCnt];
            while (enm.MoveNext())
            {
                i++;
                ListItem itm = enm.Current as ListItem;
                if (CheckLst2(itm.Value)) str[i - 1] = i.ToString();
            }
            for (int j = 0; j < iCnt; j++)
            {
                if (str[j] != null)
                {
                    lst1.Items.RemoveAt(int.Parse(str[j]) - 1 - k);
                    k++;
                }
            }
        }
    }
    /*
     * 函数名：CheckLst2
     * 功能：判断Lst2中是否存在相同数据
     * 参数：无
     * 说明：
     */
    private bool CheckLst2(string str)
    {
        if (lst2.Items.Count > 0)
        {
            IEnumerator enm = lst2.Items.GetEnumerator();
            while (enm.MoveNext())
            {
                ListItem itm = enm.Current as ListItem;
                if (itm.Value == str) return true;
            }
            return false;
        }
        else
            return false;
    }
    #endregion

    #region 设置Lst2
    /*
     * 函数名：SetLst2
     * 功能：设置Lst2
     * 参数：无
     * 说明：
     */
    private void SetLst2()
    {
        lst2.Items.Clear();
        HealthAssignBean dataBean = new HealthAssignBean();
        HealthAssignDao Dao = new HealthAssignDao();
        dataBean.DoctorId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        IList dataList = Dao.FindByMenberHave(dataBean);
        if (dataList.Count > 0)
        {
            IEnumerator enm = dataList.GetEnumerator();
            while (enm.MoveNext())
            {
                dataBean = enm.Current as HealthAssignBean;
                ListItem itm = null;
                itm = new ListItem(dataBean.MemberNo + " " + dataBean.Name, dataBean.MemberNo);
                lst2.Items.Add(itm);
            }
        }
    }
    #endregion

    #region 过滤按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {


            string floor = Request.Form["ctl00$ContentPlaceHolder1$dropFloor"].ToString();
            string room = Request.Form["ctl00$ContentPlaceHolder1$dropRoom"].ToString();
            ViewState["TXT_FLOOR"] = floor;
            ViewState["TXT_ROOM_NO"] = room;
            SalesInfoDao dao = new SalesInfoDao();
            SalesInfoBean salesInfoBean = new SalesInfoBean();
            salesInfoBean.BuildingNo = this.dropBuildNo.SelectedValue;
            //floor
            IList list = dao.FindBuildFloor(salesInfoBean);
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list = new ArrayList();
                list.Add(salesInfoBean);
            }

            this.dropFloor.DataSource = list;
            this.dropFloor.DataBind();
            this.dropFloor.SelectedValue = floor;

            //room
            salesInfoBean.Floor = floor;
            list = dao.FindBuildRoom(salesInfoBean);
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list = new ArrayList();
                list.Add(salesInfoBean);
            }

            this.dropRoom.DataSource = list;
            this.dropRoom.DataBind();
            this.dropRoom.SelectedValue = room;

            ViewState["MemberNo"] = txtMemberNo.Text.Trim();
            ViewState["Name"] = txtMemberName.Text.Trim();
            ViewState["ApartmentNo"] = dropRoom.SelectedValue;
            ViewState["BuildNo"] = dropBuildNo.SelectedValue;
            ViewState["Floor"] = dropFloor.SelectedValue;
        }
    }
    #endregion

    #region 会员分配按钮
    protected void btnAssignAdd_Click(object sender, EventArgs e)
    {
        if (lst1.SelectedIndex != -1)
        {
            ViewState["LstLoad"] = "1";
            IEnumerator enm = lst1.Items.GetEnumerator();
            int i = 0;
            while (enm.MoveNext())
            {
                i++;
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected)
                {
                    int k = itm.Text.IndexOf("(");
                    if (k > -1) itm.Text = itm.Text.Substring(0, k);
                    lst2.Items.Add(itm);
                }
            }
        }
        lst1.SelectedIndex = -1;
        lst2.SelectedIndex = -1;
    }
    #endregion

    #region 撤销分配按钮
    protected void btnAssignDel_Click(object sender, EventArgs e)
    {
        if (lst2.SelectedIndex != -1)
        {
            ViewState["LstLoad"] = "2";
            IEnumerator enm = lst2.Items.GetEnumerator();
            int i = 0;
            int k = 0;
            int iCnt = lst2.Items.Count;
            string[] str = new string[iCnt];
            while (enm.MoveNext())
            {
                if (ViewState["LIST"] != null)
                {
                    AllList = (ArrayList)ViewState["LIST"];
                }
                i++;
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected)
                {
                    str[i - 1] = i.ToString();
                    string[] strLst = new string[2];
                    strLst[0] = itm.Text;
                    strLst[1] = itm.Value;
                    if (CheckHlist(AllList, strLst[1]))
                    {
                        AllList.Add(strLst);
                    }
                }
            }

            ViewState["LIST"] = AllList;
            for (int j = 0; j < iCnt; j++)
            {
                if (str[j] != null)
                {
                    lst2.Items.RemoveAt(int.Parse(str[j]) - 1 - k);
                    k++;
                }
            }
        }
    }
    #endregion

    #region 登录按钮
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ViewState["LstLoad"] = "";
        IEnumerator enm = lst2.Items.GetEnumerator();
        IList lst = new ArrayList();
        HealthAssignDao Dao = new HealthAssignDao();
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
        string strUserId = mstSysUserBean.SysUserId;
        while (enm.MoveNext())
        {
            ListItem itm = enm.Current as ListItem;
            HealthAssignBean dataBean = new HealthAssignBean();
            dataBean.MemberNo = itm.Value;
            dataBean.NowFlg = true;
            dataBean.DoctorId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            dataBean.UserId = strUserId;
            lst.Add(dataBean);
        }
        Dao.InsertMemberDoctorLst(lst, Convert.ToString(ViewState["EMPLOYEE_ID"]), strUserId);
        ((Design_MasterPage)this.Master).InfoMessage =
            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        ViewState["LIST"] = null;
    }
    #endregion

    private bool CheckHlist(IList Alist, string str)
    {
        if (Alist.Count > 0)
        {
            for (int i = 0; i < Alist.Count; i++)
            {
                string[] str1 = (string[])Alist[i];
                if (str1[1] == str)
                    return false;
            }
            return true;
        }
        else
            return true;
    }

    private bool CheckListItem(string str)
    {
        IEnumerator enm = lst1.Items.GetEnumerator();
        while (enm.MoveNext())
        {
            ListItem itm = enm.Current as ListItem;
            if (itm.Value == str)
                return false;
        }
        return true;
    }

}
